package com.northwest_man.refactor_tool;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.IllegalFormatConversionException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 找出含占位符的行
 */
public class ListPlaceholder {


    public static void main(String[] args) {
        try {
            findPlaceholderAccording2EnglishLangFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static final String root = Constants.PAT_PROJECT_DIR + "/app/src/main/res/values-iw";
    // private static final String other = Constants.PAT_PROJECT_DIR +
    // "/app/src/main/res/";
    private static final String other = "/Users/h/Desktop/AndroidStringFiles/";


    private static void findPlaceholderAccording2EnglishLangFile()
            throws IOException {
        // %(\d+\$)?([-#+ 0,(\<]*)?(\d+)?(\.\d+)?([tT])?([a-zA-Z%])
        final String regex = "%(\\d+\\$)?([-#+ 0,(\\<]*)?(\\d+)?(\\.\\d+)?([tT])?([a-zA-Z%])";
        final String regex2 = Constants.STRING_ITEM_ROW_REGEX;
        final String enFileContent = FileUtils.readFileToString(new File(root + "/strings.xml"));
        final Pattern pattern = Pattern.compile(regex);

        final Pattern pattern2 = Pattern.compile(regex2);
        final Matcher matcher2 = pattern2.matcher(enFileContent);
        // 英语文件中含有占位符的 key
        final List<KV> valueWithPlaceholderKeyList = new ArrayList<>();
        while (matcher2.find()) {
            final String key = matcher2.group(1);
            final String value = matcher2.group(2);
            final Matcher matcher = pattern.matcher(value);
            final List<String> placeholderList = new ArrayList<>();
            while (matcher.find()) {
                placeholderList.add(matcher.group());
            }
            if (!placeholderList.isEmpty()) {
                valueWithPlaceholderKeyList.add(new KV(key, value, placeholderList));
            }
        }
        for (int i = 0; i < valueWithPlaceholderKeyList.size(); i++) {
            final KV item = valueWithPlaceholderKeyList.get(i);
            if (item.placeholderList.size() > 1) {
                if (item.placeholderList.size() > 2) {
                    try {
                        String.format(item.value, "我爱你", "年", 10000);
                    } catch (IllegalFormatConversionException e) {
                        System.out.println(String.format("&&&&&&&&&&&&&& key: [%s], value: [%s]", item.key, item.value));

                    }
                } else if (item.placeholderList.size() > 1) {
                    try {
                        String.format(item.value, "我爱你", "不");
                    } catch (IllegalFormatConversionException e) {
                        try {
                            String.format(item.value, 2, 2);
                        } catch (IllegalFormatConversionException e2) {
                            System.out.println(String.format("&&&&&&&&&&&&&& key: [%s], value: [%s]", item.key, item.value));

                        }
                    }

                } else {
                    System.out.println(String.format("&&&&&&&&&&&&&& key: [%s], value: [%s]", item.key, item.value));

                }

            } else {
                final String placeholder = item.placeholderList.get(0);
                if ("%d".equals(placeholder) || "%1$d".equals(placeholder)) {
//                    System.out.println(String.format(item.value , 10086));
                    String.format(item.value, 10086);
                } else if ("%s".equals(placeholder) || "%1$s".equals(placeholder)) {
//                    System.out.println(String.format(item.value , "我爱你中国"));
                    String.format(item.value, "我爱你中国");

                } else {
                    System.out.println(String.format("$$$$$$$$$$$$$$$$ key: [%s], value: [%s]", item.key, item.value));

                }
            }
        }
    }

    private static class KV {
        public final String key;
        public final String value;
        public final List<String> placeholderList;

        private KV(final String key, final String value, final List<String> placeholderList) {
            this.key = key;
            this.value = value;
            this.placeholderList = placeholderList;
        }
    }

}
